package com.gaogzhen.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gaogzhen.dto.CoinDto;
import com.gaogzhen.mappers.CoinDtoMapper;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gaogzhen.mapper.CoinMapper;
import com.gaogzhen.domain.Coin;
import com.gaogzhen.service.CoinService;
/**
* @description ${description}
* @author Administrator
* @date 2022-11-03 11:43
* @version 1.0
*/
@Service
public class CoinServiceImpl extends ServiceImpl<CoinMapper, Coin> implements CoinService{

    @Override
    public Page<Coin> findByPage(String title, String name, String type, String walletType, Byte status, Page<Coin> page) {
        // 按最后修改时间倒序查询
        page.addOrder(OrderItem.desc("last_update_time"));
        return page(page,
                new LambdaQueryWrapper<Coin>()
                    .eq(ObjectUtils.isNotEmpty(status), Coin::getStatus, status)
                    .eq(StringUtils.isNotEmpty(type), Coin::getType, type)
                    .eq(StringUtils.isNotEmpty(walletType), Coin::getWallet, walletType)
                    .like(StringUtils.isNotEmpty(title), Coin::getTitle, title)
                    .like(StringUtils.isNotEmpty(name), Coin::getName, name)
        );
    }

    @Override
    public List<Coin> findAll(Byte status) {
        return list(new LambdaQueryWrapper<Coin>().eq(ObjectUtils.isNotEmpty(status), Coin::getStatus, status));
    }

    @Override
    public Coin findByCoinName(String name) {
        return getOne(new LambdaQueryWrapper<Coin>()
                .eq(Coin::getName, name)
        );
    }

    @Override
    public Map<Long, CoinDto> findCoins(List<Long> coinIds) {
        List<Coin> coins = list(new LambdaQueryWrapper<Coin>().in(CollectionUtils.isNotEmpty(coinIds), Coin::getId, coinIds));
        if (CollectionUtils.isEmpty(coins)) {
            return Collections.emptyMap();
        }
        List<CoinDto> coinDtos = CoinDtoMapper.INSTANCE.convert2Dto(coins);
        return coinDtos.stream().collect(Collectors.toMap(CoinDto::getId, Function.identity()));
    }
}
